/**
 * 
 */
package adnotatio.common.xml;

import java.io.StringReader;

import junit.framework.TestCase;

import org.w3c.dom.Document;

import adnotatio.server.DOMUtil;
import adnotatio.server.xml.dom.DomXmlDocument;

/**
 * @author kotelnikov
 */
public class XMLWrapperTest extends TestCase {

    /**
     * @param name
     */
    public XMLWrapperTest(String name) {
        super(name);
    }

    private XMLDocument getDocument(String xml) throws Exception {
        Document dom = DOMUtil.readXML(new StringReader(xml));
        return new DomXmlDocument(dom);
    }

    public void test() throws Exception {
        XMLDocument doc = getDocument("<xml></xml>");
        assertNotNull(doc);
        XMLElement root = doc.getRootElement();
        assertNotNull(root);
        assertEquals("xml", root.getNodeName().getName());
        assertEquals(0, root.getChildNumber());

        doc = getDocument("<xml><a>123</a></xml>");
        assertNotNull(doc);
        root = doc.getRootElement();
        assertNotNull(root);
        assertEquals("xml", root.getNodeName().getName());
        assertEquals(1, root.getChildNumber());
        XMLNode test = root.getChild(0);
        assertNotNull(test);
        assertTrue(test instanceof XMLElement);
        XMLElement e = (XMLElement) test;
        assertEquals("a", e.getNodeName().getName());

        assertEquals(1, e.getChildNumber());
        test = e.getChild(0);
        assertNotNull(test);
        assertTrue(test instanceof XMLText);
        XMLText text = (XMLText) test;
        assertEquals("123", text.getText());
    }

    public void testNames() {
        XMLName first = new XMLName("foo:bar", "f", "toto");
        XMLName second = new XMLName("foo:bar", null, "toto");
        assertEquals(first, second);

        second = new XMLName(null, "f", "toto");
        assertFalse(first.equals(second));

        first = new XMLName(null, "f", "toto");
        assertTrue(first.equals(second));

        second = new XMLName(null, null, "toto");
        assertFalse(first.equals(second));

        first = new XMLName(null, null, "toto");
        assertTrue(first.equals(second));
    }

}
